package cn.tjjcy.common;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.tika.Tika;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.ParserDecorator;
import org.apache.tika.parser.epub.EpubParser;
import org.apache.tika.parser.html.HtmlParser;
import org.apache.tika.parser.iwork.IWorkPackageParser;
import org.apache.tika.parser.microsoft.OfficeParser;
import org.apache.tika.parser.microsoft.OldExcelParser;
import org.apache.tika.parser.microsoft.ooxml.OOXMLParser;
import org.apache.tika.parser.odf.OpenDocumentParser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.parser.rtf.RTFParser;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.parser.xml.DcXMLParser;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Slf4j
public class FileDocUtils {
    private static final Set<MediaType> EXCLUDES = new HashSet(
            Arrays.asList(
                    MediaType.application("vnd.ms-visio.drawing"),
                    MediaType.application("vnd.ms-visio.drawing.macroenabled.12"),
                    MediaType.application("vnd.ms-visio.stencil"),
                    MediaType.application("vnd.ms-visio.stencil.macroenabled.12"),
                    MediaType.application("vnd.ms-visio.template"),
                    MediaType.application("vnd.ms-visio.template.macroenabled.12"),
                    MediaType.application("vnd.ms-visio.drawing")
            ));

    private static final Parser[] PARSERS = {
            new HtmlParser(),
            new RTFParser(),
            new PDFParser(),
            new TXTParser(),
            new OfficeParser(),
            new OldExcelParser(),
            ParserDecorator.withoutTypes(new OOXMLParser(), EXCLUDES),
            new OpenDocumentParser(),
            new IWorkPackageParser(),
            new DcXMLParser(),
            new EpubParser()
    };

    private static final AutoDetectParser PARSER_INSTANCE = new AutoDetectParser(PARSERS);

    private static final Tika TIKA_INSTANCE = new Tika(PARSER_INSTANCE.getDetector(), PARSER_INSTANCE);

    public static String parseToString(InputStream stream) {
        String parsedContent = "";
        try {
            Metadata metadata = new Metadata();
            parsedContent = TIKA_INSTANCE.parseToString(stream, metadata);
            return parsedContent;
        } catch (Exception e) {
            log.error("FileDocUtils parseToString error", e);
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
                log.error("FileDocUtils parseToString error", e);
            }
        }
        return parsedContent;
    }

    public static String parseToString(File file) {
        BufferedInputStream stream = FileUtil.getInputStream(file);
        return parseToString(stream);
    }


    public static String readLine(Integer line, File file) {
        try {
            BufferedInputStream stream = FileUtil.getInputStream(file);
            Metadata metadata = new Metadata();
            String content = TIKA_INSTANCE.parseToString(stream, metadata);
            String[] lines = content.split(System.lineSeparator());
            if (lines.length == 0 || lines.length < line) {
                return "";
            }
            return String.join(System.lineSeparator(), Arrays.copyOfRange(lines, 0, line));
        } catch (Exception e) {
            log.error("FileDocUtils readLine error", e);
        }
        return "";
    }

}
